<template>
	<loading v-if="0 != loading" :status="loading" />
	<view v-else class="main-view">
		<block v-if="listData && listData.length > 0">
			<block v-for="(item, index) in listData" :key="index">
				<view class="list-item" @tap="changePlat" :data-item="item">
					<text>{{ item.platName }}</text>
					<u-icon name="checkbox-mark" size="40rpx" color="red"
						v-if="item.ifCurrentPlat == '1'"></u-icon>
				</view>
			</block>
		</block>
		
	</view>
</template>
<script module="util" lang="wxs" src="../../utils/util.wxs"></script>
<script>
	const app = getApp();
	import loading from '@/components/loading/loading';
	import {
		baseImgUrl,
		baseUrl
	} from '../../utils/constant.js'
	import {
		http
	} from '@/utils/commonRequest.js'; // 局部引入
	const util = require('../../utils/util.js');


	export default {
		components: {
			loading
		},
		data() {
			return {		
				platformConfig: app.globalData.platformConfig,
				baseImgUrl: baseImgUrl,
				baseUrl: baseUrl,
				loading: 1, // 1加载中,0加载成功,-1加载失败
				id: '',
				listData: [],
				currentId: undefined
			}
		},
		onLoad(options) {
			let id = options && options.id;
			this.id = id;
			this.$checkLogin().then(res => {
				this.loadData();
			})
		},
		
		// onUnload() {
		// 	uni.reLaunch({
		// 		url: '/pages/index/index'
		// 	});
		// },
		
		methods: {
			loadData: function() {
				let that = this;
				http.get('/mtBank/getPlatListAll', {})
				.then((res) => {
					res = res.data
					if ('200' == res.code) {
						that.loading = 0;
						that.listData = res.platList;				
						res.platList.forEach((item) => {
							if(item.ifCurrentPlat == '1'){
								that.currentId = item.id;
							}
						})
					}
				})
				.catch((err) => {
					this.loading = -1
					console.log('err===',err);
				});
			},
			
			/** 切换平台 */
			changePlat: function(e) {
				let that = this;
				let item = e.currentTarget.dataset.item;
				let id = item.id;
				let platName = item.platName;
				util.confirm('是否切换到' + platName + '?', (res) => {
					if (res.confirm) {
						if (that.currentId == id) {
							util.tips('您正在看该区域平台，无需切换!');
							return;
						}
						that.changeSession(id,item)
					} else {
						console.log('取消');
					}
				});
			},
			
			changeSession: async function(id,item) {
				let params = {
					targetPlatId: id
				};
				let res = await this.$http.post("/levelstructure/changeSession", params, {
					header: {
						"content-type": "application/x-www-form-urlencoded"
					}
				})
				if (res.data.code == '200') {
					util.showLoading('切换中,请稍后~');
					setTimeout(() => {
						util.hideLoading();
						if (item.latitude) {
							app.globalData.currLatitude = item.latitude;
							app.globalData.currLongitude = item.longitude;
							app.globalData.platLat = item.latitude;
							app.globalData.platLng = item.longitude;
						} else if (item.platLatitude) {
							app.globalData.currLatitude = item.platLatitude;
							app.globalData.currLongitude = item.platLongitude;
						}
						app.globalData.platFlag = '2';
						app.globalData.platId = id;
						uni.$emit('plantChange')
						uni.navigateBack({
							delta: 1
						});
					}, 1000);
				}
			}

		}
	}
</script>

<style>
	@import '/iconfont/iconfont.css';
	.main-view{
		padding: 24rpx;
	}
	
	
	.list-item{
		display: flex;
		align-items: center;
		justify-content: space-between;
		width: 100%;
		padding: 50rpx 32rpx;
		background-color: #fff;
		border-radius: 10rpx;
		margin-bottom: 4rpx;
		box-shadow: 0 20rpx 60rpx 0 rgba(112,136,210,0.1);
	}


	
</style>
